package cn.edu.sdjzu.xg.bysj.service;
//201902104023赵扬

import cn.edu.sdjzu.xg.bysj.dao.DepartmentDao;
import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.School;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class DepartmentService {
    private static DepartmentDao departmentDao= DepartmentDao.getInstance();
    private static DepartmentService departmentService=new DepartmentService();
    private DepartmentService(){}

    public static DepartmentService getInstance(){
        return departmentService;
    }

    /**
     * 根据来自前端的请求向departmentDao发送消息，查询对应的元组并返回对象集
     * @param condition_json
     * @return
     * @throws SQLException
     */
    public Collection<Department> findAll(Collection<Condition> condition_json, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //根据来自前端请求向departmentDao发送消息，查询对应的对象集合
        Collection<Department> departments = departmentDao.findAll(connection,condition_json,pagination);
        //返回查找到的对象集合
        JdbcHelper.close(connection);
        return departments;
    }

    /**
     * 根据id向departmentDao发消息获得对应id的元组的对象
     * @param id
     * @return
     * @throws SQLException
     */
    public Department find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Department department = departmentDao.find(connection,id);
        JdbcHelper.close(connection);
        return department;
    }

    /**
     * 向departmentDao发消息修改元组
     * @param department
     * @return
     * @throws SQLException
     */
    public boolean update(Department department) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = departmentDao.update(connection,department);
        JdbcHelper.close(connection);
        return updated;
    }

    /**
     * 向departmentDao发消息增加元组
     * @param department
     * @return
     * @throws SQLException
     */
    public boolean add(Department department) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean added = departmentDao.add(connection,department);
        JdbcHelper.close(connection);
        return added;
    }

    /**
     * 向departmentDao发消息删除元组
     * @param id
     * @return
     * @throws SQLException
     */
    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = departmentDao.delete(connection,id);
        JdbcHelper.close(connection);
        return  deleted;
    }

    public boolean delete(Department department) throws SQLException {
        return this.delete(department.getId());
    }

    /**
     * 向departmentDao发消息获得对应学院下属系数量
     * @param school
     * @return
     * @throws SQLException
     */
    public int countAll(School school) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        int allDepartment = departmentDao.countAll(connection,school);
        return allDepartment;
    }
}

